<html>
<head>
<title>canvas read data url of an image. (Adapted from LayoutTests)</title>
<script type="text/javascript" src="/shared/scripts/testcase.js"></script>
<script type="text/javascript">
if (window.addEventListener) {
	window.addEventListener('load', f, false);
} else if (window.attachEvent) {
	window.attachEvent('onload', f);
} else {
	window.onload = f;
}

function testGetImageData(context, description, tc)
{
    tc.output += "Calling getImageData() from a canvas tainted by a " + description + '\n';
    try {
        var imageData = context.getImageData(0,0,100,100);
        tc.output += description + " was allowed: " + imageData + '\n';
		tc.result = 'readable\n';
		// shouldn't be allowed to read a tainted canvas.
		tc.test_passed = 'false'; 
    } catch (e) {
        tc.output += description + " was not allowed - Threw error: " + e + ".\n";
    }
	
}

function testToDataURL(canvas, description, tc)
{
    tc.output += "Calling toDataURL() on a canvas tainted by a " + description + '\n';
    try {
        var dataURL = canvas.toDataURL();
        tc.output += description + " was allowed: [data len:" + dataURL.length + ']\n';
		tc.output += dataURL;
		tc.result = 'readable\n';
		// shouldn't be allowed to read a tainted canvas.
		tc.test_passed = 'false';
    } catch (e) {
        tc.output += description + " was not allowed - Threw error: " + e + ".\n";
    }
}

function run_tests(canvas, description, tc)
{
    testGetImageData(canvas.getContext("2d"), description, tc);
    testToDataURL(canvas, description, tc);
}


function f() {
	var tc = new TestCase();
	tc.input = 'toDataURL(); getImageData();';
	tc.description = 'canvas read remote image data after redirect. (Adapted from LayoutTests)';
	tc.expected_result = "unreadable or unsupported.";
	tc.test_passed = 'true';
	
	var image = new Image();
	image.onload = function() {
		var canvas = null;
		var context = null;
		try {
			canvas = document.createElement("canvas");
			canvas.width = 100;
			canvas.height = 100;
			context = canvas.getContext("2d");
			// Test reading from a canvas after drawing a data URL image onto it
			context.drawImage(image, 0, 0, 100, 100);
		} catch(e) {
			tc.result = 'unsupported';
			tc.saveTest();
			return;
		}
		
		run_tests(canvas, "drawImage of 3rd party img type", tc);

		// Test reading after using a data URL pattern
		canvas = document.createElement("canvas");
		canvas.width = 100;
		canvas.height = 100;
		var context = canvas.getContext("2d");
		var remoteImagePattern = context.createPattern(image, "repeat");
		context.fillStyle = remoteImagePattern;
		context.fillRect(0, 0, 100, 100);

		run_tests(canvas,"createPattern of 3rd party img type", tc);
		if (tc.result == '') {
			tc.result = 'unreadable';
		}
		tc.saveTest();

	}
	// A small 4x4 img.
	image.src = "/redirect?loc=http://victim.com/shared/resources/4x4.png";
}
</script>
</head>
<body></body>
</html>
